package com.nus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.nus.parsers.ParserAPIs;
import com.nus.representations.IvleCodeTableWeekType;
import com.nus.representations.IvleModule;
import com.nus.representations.IvleRosters;

public class IvleCodeWeekType {
	private ListView lv;

	private String lv_arr[];

	public static ArrayList<IvleCodeTableWeekType> week_type;

	public void getWeekCodeData() {

		// super.onCreate(savedInstanceState);
		// setContentView(R.layout.ivlerosterlayout);
		String url = "https://ivle.nus.edu.sg/api/Lapi.svc/CodeTable_WeekTypes?APIKey=W7RtgW7GZKUpaEkJ1NwUD"
				+ "&output=json";
		try {
			String response = getResponseText(url);

			Log.i("Response", response);

			week_type = ParserAPIs.IvleWeekCode(response);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// lv=(ListView)findViewById(R.id.list_view);
		// lv.setAdapter(new ListAdapter(IvleRosterActivity.this,R.id.rosterLV,
		// rosters));

	}

	public String getResponseText(String stringUrl) throws IOException {
		StringBuilder response = new StringBuilder();

		URL url = new URL(stringUrl);
		HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
		if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					httpconn.getInputStream()), 8192);
			String strLine = null;
			while ((strLine = input.readLine()) != null) {
				response.append(strLine);
			}
			input.close();
		}
		return response.toString();
	}

	// private class ListAdapter extends ArrayAdapter<IvleModule> { // --
	// // CloneChangeRequired
	// private ArrayList<IvleModule> mList; // --CloneChangeRequired
	// private Context mContext;
	//
	// public ListAdapter(Context context, int textViewResourceId,
	// ArrayList<IvleModule> list) { // --CloneChangeRequired
	// super(context, textViewResourceId, list);
	// this.mList = list;
	// this.mContext = context;
	// }
	//
	// public View getView(int position, View convertView, ViewGroup parent) {
	// View view = convertView;
	// try {
	// if (view == null) {
	// LayoutInflater vi = (LayoutInflater)
	// getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	// view = vi.inflate(R.layout.ivlemoduleitem, null);
	// }
	// final IvleModule listItem = mList.get(position);
	// if (listItem != null) {
	// // setting list_item views
	// TextView tv = (TextView) view.findViewById(R.id.module_name);
	// tv.setText(listItem.getCourseCode());
	// TextView tv2 = (TextView) view.findViewById(R.id.semester);
	// tv2.setText(listItem.getCourseSemester());
	// tv2.setTextColor(Color.BLUE);
	// TextView tv3 = (TextView) view.findViewById(R.id.acad_year);
	// tv3.setText(listItem.getCourseAcadYear());
	// tv3.setTextColor(Color.BLUE);
	// view.setOnClickListener(new OnClickListener() {
	// public void onClick(View arg0) { // --clickOnListItem
	// Intent myIntent = new Intent(IvleModuleActivity.this,
	// IvleModuleOptionsActivity.class);
	// COURSE_ID = listItem.getCourseId();
	// COURSE_ACAD_YEAR=listItem.getCourseAcadYear();
	// SEMESTER_ID=listItem.getCourseSemester().substring(9);
	// Log.i("modules", "module semester "+SEMESTER_ID);
	// Log.i("module acad year"," acad year "+COURSE_ACAD_YEAR);
	// startActivity(myIntent);
	// finish();
	// }
	// });
	// }
	// } catch (Exception e) {
	// Log.i(IvleModuleActivity.ListAdapter.class.toString(), e.getMessage());
	// }
	// return view;
	// }
	// }
}
